package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *post方法
 */
public class Ajax3Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 * pageEncoding="UTF-8"的作用是设置JSP编译成Servlet时使用的编码。 
			contentType="text/html;charset=UTF-8"的作用是指定对服务器响应进行重新编码的编码。 
		 * request.setCharacterEncoding("UTF-8")的作用是设置对客户端请求进行重新编码的编码。
			response.setCharacterEncoding("UTF-8")的作用是指定对服务器响应进行重新编码的编码。
		 */
		System.out.println(request.getMethod());
		request.setCharacterEncoding("utf-8");   //对客户端的请求进行编码
		
		//接收参数
		String username=request.getParameter("username");
		System.out.println(username);
		
		response.setContentType("text/html;charset=utf-8");   //对指定的服务器进行编码
		response.getWriter().println("post姓名="+username);
	}

}
